package com.foreknow.dao.impl;

import com.foreknow.bean.Admin;
import com.foreknow.dao.AdminDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    @Override
    public Admin login(String username, String password) {
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.ADMIN_MAPPING);
        List<Object> list = jdbcTemplate.query("select * from admin where username=? and password=?",mapping,username,password);
        if(list.size() > 0) {
            return (Admin) list.get(0);
        }
        return null;
    }

    @Override
    public boolean register(Admin admin)throws SQLException {
        //调用模板类中的方法向数据库中添加数据
        int isRight = jdbcTemplate.update("insert into admin(username,password) values(?,?)",
                admin.getUsername(),admin.getPassword());
        return isRight == 1;
    }

}
